﻿// ------------------------------------------------------------------------------------------------
//  <copyright file="IndexTree.cs" company="Iveely">
//    Copyright (c) Iveely Liu.  All rights reserved.
//  </copyright>
//  
//  <Create Time>
//    03/02/2013 21:59 
//  </Create Time>
//  
//  <contact owner>
//    liufanping@iveely.com 
//  </contact owner>
//  -----------------------------------------------------------------------------------------------

#region

using System;

#endregion

namespace IveelySE.Common.Html
{
    /// <summary>
    ///   树结点
    /// </summary>
    [Serializable]
    public class Node
    {
        /// <summary>
        ///   构造方法
        /// </summary>
        public Node()
        {
            //实例化孩子结点
            this.Children = new SortedList<Node>();
        }

        /// <summary>
        ///   结点名
        /// </summary>
        public int Name { get; set; }

        /// <summary>
        ///   父节点
        /// </summary>
        public Node Parent { get; set; }

        /// <summary>
        ///   孩子集合
        /// </summary>
        public SortedList<Node> Children { get; set; }
    }

    /// <summary>
    ///   索引树
    /// </summary>
    [Serializable]
    public class IndexTree
    {
        /// <summary>
        ///   根结点
        /// </summary>
        protected Node Root;

        /// <summary>
        ///   构造方法
        /// </summary>
        public IndexTree()
        {
            // 初始化
            this.Root = new Node();

            // 父结点为0
            this.Root.Parent = null;
        }

        /// <summary>
        ///   查找某一个结点
        /// </summary>
        /// <param name="id"> 结点值 </param>
        /// <param name="list"> </param>
        /// <returns> </returns>
        public Node Find(int id, Node list)
        {
            // 遍历结点的值
            foreach(Node child in list.Children)
            {
                // 如果名称相等
                if(child.Name == id)
                {
                    return child;
                }

                // 否则继续深度遍历
                // 遍历孩子
                return this.Find(id, child);
            }

            return null;
        }
    }
}